package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserApiImpl implements UserApi {
    @Autowired
    private UserMapper userMapper;

    /**
     * 修改手机号
     * @param userId
     * @param phone
     */
/*    @Override
    public void updatePhone(Long userId, String phone) {
        User user = new User();
        user.setMobile(phone);
        user.setId(userId);
        userMapper.updateById(user);
    }*/

    /**
     * 查询手机号是否存在数据库中
     * @param phone
     * @return
     */
    @Override
    public Boolean getPhone(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,phone);
        Integer count = userMapper.selectCount(queryWrapper);
        return count==0;
    }

    //查看环信账户信息
    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public void updatePhone(User user) {
        userMapper.updateById(user);
    }

    //更新环信账号
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    //设置所有老用户的环信账号
    @Override
    public List<User> findAll() {
        return userMapper.selectList(Wrappers.emptyWrapper());
    }

    //根据环信用户id，查询用户信息
    @Override
    public User findByHuanXin(String huanxinId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hx_user",huanxinId);
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    @Override
    public User findByMobile(String mobile) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        return userMapper.selectOne(wrapper);
    }

    /**
     * 保存用户，返回用户id
     * @param user
     * @return
     */
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }
}
